package com.example.chenhuamou.freenews.view.news;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.chenhuamou.freenews.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chenhuamou on 2017/9/19.
 */

public class NewsListFragment extends Fragment {

    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView mRecycleView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_list, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        String[] data = {"萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱"

        };

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        mRecycleView.setLayoutManager(llm);
        mRecycleView.setHasFixedSize(true);
        mRecycleView.setAdapter(new MyAdapter(data));
    }


    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        public String[] datas = null;
        public MyAdapter(String[] datas) {
            this.datas = datas;
        }
        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_news_list,viewGroup,false);
            ViewHolder vh = new ViewHolder(view);
            return vh;
        }
        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.mTextView.setText(datas[position]);
        }
        //获取数据的数量
        @Override
        public int getItemCount() {
            return datas.length;
        }
        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public  class ViewHolder extends RecyclerView.ViewHolder {
            public TextView mTextView;
            public ViewHolder(View view){
                super(view);
                mTextView = (TextView) view.findViewById(R.id.text);
            }
        }
    }





}

